package com.yhf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yhf.bean.Admin;
import com.yhf.bean.Clazz;
import com.yhf.bean.Grade;
import com.yhf.bean.Teacher;
import com.yhf.service.ClazzService;
import com.yhf.service.TeacherService;
import com.yhf.utils.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
    private Map<String,Object> result = new HashMap<>();
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private ClazzService clazzService;

    @GetMapping("/goTeacherListView")
    public String goTeacherListView(Model model){
        List<Clazz> clazzList = clazzService.getClazzList(new Clazz());
        model.addAttribute(clazzList);
        return "teacher/teacherList";
    }
    @PostMapping("/getTeacherList")
    @ResponseBody
    public Map<String,Object> getTeacherList(Integer page,Integer rows,String teachername,String clazzname){
        Teacher teacher = new Teacher();
        teacher.setName(teachername);
        teacher.setClazz_name(clazzname);
        //设置每页记录数
        PageHelper.startPage(page,rows);
        //获取指定的和管理员或者是所有的管理员数据
        List<Teacher> teacherList = teacherService.getTeacherList(teacher);
        PageInfo<Teacher> teacherPageInfo = new PageInfo<>(teacherList);
        //总页数
        long total = teacherPageInfo.getTotal();
        result.put("total",total);
        result.put("rows",teacherList);
        return result;
    }
    @PostMapping("/addTeacher")
    @ResponseBody
    public Map<String,Object> addTeacher(Teacher teacher){
            //添加的管理员用户名是否存在
            //调用adminService完成
            Teacher user = teacherService.findByName(teacher.getName());
            if (user == null) {
                //添加操作
                try {
                    if (teacherService.addTeacher(teacher) > 0) {
                        result.put("success", true);
                    } else {
                        result.put("success", false);
                        result.put("msg", "服务器发生异常,添加失败!");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                result.put("success", false);
                result.put("msg", "该用户名已存在,请修改后重试!");
            }
            return result;
        }
    @PostMapping("/editTeacher")
    @ResponseBody
    public Map<String, Object> editteacher(Teacher teacher) {
        //需要排除用户名只修改账户名以外信息
        Teacher teacher1 = teacherService.findByName(teacher.getName());
        if (teacher1 != null) {
            if (!(teacher.getId().equals(teacher1.getId()))) {
                result.put("success", false);
                result.put("msg", "该用户名已存在，请更改重试！");
                return result;
            }
        }
            //修改操作
            if (teacherService.updateTeacher(teacher) > 0) {
                result.put("success", true);
            } else {
                result.put("success", false);
                result.put("msg", "服务器异常，修改失败！");
            }
            return result;

        }
        @PostMapping("/deleteTeacher")
        @ResponseBody
        public Map<String, Object> deleteTeacher(@RequestParam(value = "ids[]", required = true) Integer[] ids) {
            if (teacherService.deleteTeacher(ids) > 0) {
                result.put("success", true);
            } else {
                result.put("success", false);
                result.put("msg", "服务器异常，删除失败！");
            }
            return result;
        }
        @PostMapping("/uploadPhoto")
        @ResponseBody
        public Map<String,Object> uploadPhoto(MultipartFile photo, HttpServletRequest request){
            final  String realPath = request.getServletContext().getRealPath("/uplod/admin_portrait/");
            final String portraitPath = request.getServletContext().getContextPath() + "/uplod/admin_portrait/";
            return UploadFile.getUploadResult(photo,realPath,portraitPath);
        }
}
